package edu.mit.media.fv.components
{
	import flash.display.Sprite;
	import flash.text.*;
	import flash.utils.Dictionary;
	
	public class FVBarGraph extends Sprite
	{
		private var barheight:int = 25;
		private var barwidth:int = 200;
		private var margin:int = 10;
		
		private var _width:int;
		private var _height:int;
		
		private var viewport:Sprite;
		
		public function FVBarGraph(x:int = 0, y:int = 0, width:int = 100, height:int = 100){
			super();
			this._height = height;
			this._width = width;
			this.x= x;
			this.y = y;
		}
		public function setValues(d:Dictionary):void{
			if(this.viewport != null){
				this.removeChild(this.viewport);
			}
			this.viewport = new Sprite();
			this.addChild(this.viewport);
			
			var max:Number = 0;
			var count:int = 0;
			for(var element:Object in d){
				var v:Number = d[element];
				if(v>max){
					max = v;
				}
				count = count+1;
			}
			
			this.barheight = this._height/(count)-margin;
			
			var i:int = 0;
			
			
			this.viewport.graphics.clear();
			this.viewport.graphics.lineStyle(3, 0x000000);
			this.viewport.graphics.moveTo(0, 0);
			this.viewport.graphics.lineTo(0, _height);
			this.viewport.graphics.moveTo(0,0);
			this.viewport.graphics.beginFill(0xFFFFFF, 1.0);
			this.viewport.graphics.lineStyle(1, 0x000000);
			
			for(var text:String in d){
				var value:Number = Number(d[text]);
				this.viewport.graphics.drawRect(
					0,
					i*(barheight+margin),
					value/max*barwidth,
					barheight);
				var field:FVLabel = new FVLabel(
					value/max*barwidth+25,
					i*(barheight+margin),
					650,
					barheight,
					value+" : "+text, TextFormatAlign.LEFT);
			 	this.viewport.addChild(field);
				i = i+1;
			}
		}

	}
}